package cn.m3a1.control.master;

import cn.m3a1.bean.AbsentNote;
import cn.m3a1.bean.Msg;
import cn.m3a1.bean.Tclass;
import cn.m3a1.bean.User;
import cn.m3a1.service.master.MasterMyclassService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author wyj
 * @date 2019/7/10
 * @time 上午10:35
 */
@Controller
@RequestMapping("/head")
public class HeadController {
    @Autowired
    MasterMyclassService masterMyclassService;

    @RequestMapping("/home")
    public String home(ModelMap modelMap, HttpServletRequest request) {
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("loginUser");
        List<Tclass> allClass = masterMyclassService.getAllClass(user.getId());
        // List<Tclass> allClass = masterMyclassService.getAllClass(202);
        modelMap.put("tlist", allClass);
        modelMap.put("loginUser", user);

        return "head/Myclass";
    }

    @RequestMapping("/stuHome")
    public String StudentHome(ModelMap modelMap, int id) {
        List<User> userByTclass = masterMyclassService.getUserByTclass(id);
        modelMap.put("members", userByTclass);
        return "head/Student";
    }

    @RequestMapping("/headLeave")
    public String headLeave(ModelMap modelMap, HttpServletRequest request) {
        HttpSession session = request.getSession();
        User loginUser = (User) session.getAttribute("loginUser");
        List<AbsentNote> absentNotes = masterMyclassService.getByMaster(loginUser.getId());
        System.out.println(absentNotes.size());
        modelMap.put("absentnote", absentNotes);
        modelMap.put("loginUser",loginUser);
        return "head/headleave";
    }

    @ResponseBody
    @RequestMapping("/approvalLeave")
    public Msg ApprovalLeave(AbsentNote absentNote,HttpServletRequest request) {
        HttpSession session = request.getSession();
        User loginUser = (User) session.getAttribute("loginUser");
        System.out.println(absentNote.getManagermark());
        absentNote.setManagerid(loginUser.getId());

        //根据班主任是否同意修改假单的状态
        if (absentNote.getManagercfm()==1){
            absentNote.setStatus(1);
        }else if (absentNote.getManagercfm()==2){
            absentNote.setStatus(2);
        }

        AbsentNote absentNote1 = masterMyclassService.getAbsentNote(absentNote.getId());
        if (absentNote1.getType()==1){
            absentNote.setStatus(2);
        }

        boolean b = masterMyclassService.setApproval(absentNote);
        return Msg.success();


    }
}
